import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/common/size_fit.dart';
import 'package:industry_net/base/component/component.dart';
import 'controller.dart';
import 'state.dart';
import 'views/content/page.dart';

class NewsPage extends StatefulWidget {
  const NewsPage({Key? key}) : super(key: key);

  @override
  _NewsPageState createState() => _NewsPageState();
}

class _NewsPageState extends State<NewsPage>
    with SingleTickerProviderStateMixin {
  final controller = Get.put(NewsController());
  late NewsState state;
  @override
  void initState() {
    super.initState();
    state = controller.state;
    state.tabCtrl = TabController(length: state.titles.length, vsync: this)
      ..addListener(() {
        if (state.tabCtrl.indexIsChanging) {
          if (state.tabCtrl.index != state.tabCtrl.previousIndex) {
            Get.log('${state.tabCtrl.index}');
          }
        }
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorsUtil.white,
      body: Stack(
        children: [
          Positioned(
              width: Get.width,
              height: 156.r,
              child: Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage('news_title_bg.png'.imgPathNews),
                        fit: BoxFit.cover)),
                padding: EdgeInsets.fromLTRB(
                    gap12, SizeFit.statusBarHeight, gap12, 0),
                child: Column(
                  children: [
                    searchFadeBar(
                      hintText: '请输入关键词',
                      width: Get.width - 2 * gap12,
                      backgroundColor: Colors.white30,
                    ),
                    SizedBox(
                      height: 10.r,
                    ),
                    SizedBox(
                      height: 30.r,
                      child: TabBar(
                        isScrollable: true,
                        indicatorColor: Colors.white,
                        indicatorPadding:
                            EdgeInsets.symmetric(horizontal: 25.r),
                        unselectedLabelStyle: pfMediumStyle(
                            color: Colors.white70, fontSize: font15),
                        labelStyle: pfSCStyle(
                            color: ColorsUtil.white, fontSize: font18),
                        controller: controller.state.tabCtrl,
                        tabs: controller.state.titles
                            .asMap()
                            .map((key, value) => MapEntry(
                                key,
                                Tab(
                                  text: value,
                                )))
                            .values
                            .toList(),
                      ),
                    ),
                  ],
                ),
              )),
          Positioned.fill(
            top: 120.r,
            child: TabBarView(
              controller: state.tabCtrl,
              children: state.titles
                  .asMap()
                  .map((key, value) => MapEntry(
                      key,
                      ContentPage(
                        index: key,
                      )))
                  .values
                  .toList(),
            ),
          ),
        ],
      ),
    );
  }
}

// class NewsPage extends StatefulWidget {
//   const NewsPage({Key? key}) : super(key: key);

//   @override
//   _NewsPageState createState() => _NewsPageState();
// }

// class _NewsPageState extends State<NewsPage>
//     with SingleTickerProviderStateMixin {
//   final controller = Get.put(NewsController());
//   late NewsState state;
//   @override
//   void initState() {
//     super.initState();
//     state = controller.state;
//     state.tabCtrl = TabController(length: state.titles.length, vsync: this)
//       ..addListener(() {
//         if (state.tabCtrl.indexIsChanging) {
//           if (state.tabCtrl.index != state.tabCtrl.previousIndex) {
//             Get.log('${state.tabCtrl.index}');
//           }
//         }
//       });
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         elevation: 0,
//         backgroundColor: ColorsUtil.white,
//         title: Text(
//           '活动资讯',
//           style: pfMediumStyle(color: ColorsUtil.title, fontSize: font18),
//         ),
//         flexibleSpace: Container(
//           color: Colors.green,
//         ),
//         bottom: PreferredSize(
//             child: TabBar(
//               isScrollable: true,
//               indicatorColor: Colors.white,
//               indicatorPadding: EdgeInsets.symmetric(horizontal: 25.r),
//               unselectedLabelStyle:
//                   pfMediumStyle(color: Colors.white70, fontSize: font15),
//               labelStyle: pfSCStyle(color: ColorsUtil.white, fontSize: font18),
//               controller: controller.state.tabCtrl,
//               tabs: controller.state.titles
//                   .asMap()
//                   .map((key, value) => MapEntry(
//                       key,
//                       Tab(
//                         text: value,
//                       )))
//                   .values
//                   .toList(),
//             ),
//             preferredSize: Size.fromHeight(50.r)),
//       ),
//       backgroundColor: ColorsUtil.white,
//       body: TabBarView(
//         controller: state.tabCtrl,
//         children: state.titles
//             .asMap()
//             .map((key, value) => MapEntry(
//                 key,
//                 ContentPage(
//                   index: key,
//                 )))
//             .values
//             .toList(),
//       ),
//     );
//   }
// }
